package com.mr.dao.service.impl;

import java.util.List;

import org.hibernate.classic.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;

import com.mr.dao.common.CommonDao;
import com.mr.dao.entity.BaseEntity;
import com.mr.dao.service.EntityService;

@Service
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public class EntityServiceImpl<T extends BaseEntity> implements EntityService<T> {

	@Autowired
	protected CommonDao commonDao;

	/**
	 * Load a object by id.
	 * @param id
	 * @return
	 */
	@Override
	public T load(Class<T> clazz, String id) {
		return StringUtils.isBlank(id) ? null : commonDao.load(clazz, id);
	}

	/**
	 * Load a object by a regular property and its value.
	 * @param propertyName
	 * @param propertyValue
	 * @return
	 */
	@Override
	public T load(Class<T> clazz, String propertyName, Object propertyValue) {
		return commonDao.load(clazz, propertyName, propertyValue);
	}

	/**
	 * Load a object by a set properties and their appropriate values.
	 * @param propertyNames
	 * @param propertyValues
	 * @return
	 */
	@Override
	public T load(Class<T> clazz, String[] propertyNames, Object[] propertyValues) {
		return commonDao.load(clazz, propertyNames, propertyValues);
	}

	/**
	 * Query all objects.
	 * @param orderProperties
	 * @return
	 */
	public List<T> findAll(Class<T> clazz, String... orderProperties) {
		return commonDao.findAll(clazz, orderProperties);
	}

	/**
	 * Query objects by a property and its value.
	 * @param propertyName
	 * @param propertyValue
	 * @return
	 */
	@Override
	public List<T> find(Class<T> clazz, String propertyName, Object propertyValue) {
		return commonDao.find(clazz, propertyName, propertyValue);
	}

	/**
	 * Query objects by a set properties and their appropriate values.
	 * @param propertyNames
	 * @param propertyValues
	 * @return
	 */
	@Override
	public List<T> find(Class<T> clazz, String[] propertyNames, Object[] propertyValues) {
		return commonDao.find(clazz, propertyNames, propertyValues);
	}

	/**
	 * Save or Update a object.
	 * @param o
	 */
	@Transactional(readOnly = false)
	@Override
	public void save(T o) {
		commonDao.save(o);
	}

	@Override
	public void delete(T o) {
		throw new UnsupportedOperationException();
	}

	@Override
	public Session getSession() {
		return commonDao.getSession();
	}
}
